package org.esk.dablog.web.forms.user;

import org.springframework.validation.Validator;
import org.springframework.validation.Errors;
import org.esk.dablog.model.Author;
import org.esk.dablog.service.UserManager;
import org.apache.commons.lang.StringUtils;

import java.util.regex.Pattern;

/**
 * This class
 * User: esk
 * Date: 10.12.2006
 * Time: 23:02:57
 * $Id:$
 */
public class UserFormValidator implements Validator {

    private UserManager userManager;
    private static final Pattern USERNAME_PATTERN = Pattern.compile("[A-Za-z0-9_]*");
    private static final Pattern EMAIL_PATTERN = Pattern.compile("[A-Za-z0-9@._-]*");

    /**
     * checks if we are adding new user or editing existing one
     */
    private boolean newUser;

    public boolean supports(Class clazz) {
        return Author.class.isAssignableFrom(clazz);
    }

    public void validate(Object obj, Errors errors) {
        Author a = (Author) obj;
        //new user, check for duplicate name
        Author existing = userManager.getAuthorByUsername(a.getUsername());
        if (newUser && existing != null) {
            errors.rejectValue("username", "app.user.profile.username.exists");
        }
        // the case: renaming user, but new name already belongs to another user
        if (!newUser && existing != null && existing.getId() != a.getId()) {
            errors.rejectValue("username", "app.user.profile.username.exists");
        }

        if (StringUtils.isBlank(a.getUsername())) {
            errors.rejectValue("username", "app.user.profile.username.empty");
        }

        if (!USERNAME_PATTERN.matcher(a.getUsername()).matches()) {
            errors.rejectValue("username", "app.user.profile.username.format");
        }

        if (!EMAIL_PATTERN.matcher(a.getEmail()).matches()) {
            errors.rejectValue("username", "app.user.profile.email.format");
        }

        if (StringUtils.isBlank(a.getFirstName())) {
            errors.rejectValue("firstName", "app.user.profile.firstname.empty");
        }

        if (!StringUtils.isAlpha(a.getFirstName())) {
            errors.rejectValue("firstName", "app.user.profile.firstname.format");
        }

        if (StringUtils.isBlank(a.getLastName())) {
            errors.rejectValue("lastName", "app.user.profile.lastname.empty");
        }

        if (!StringUtils.isAlpha(a.getLastName())) {
            errors.rejectValue("lastName", "app.user.profile.lastname.format");
        }

        if (StringUtils.isBlank(a.getCity())) {
            errors.rejectValue("city", "app.user.profile.city.empty");
        }

        if (newUser && StringUtils.isBlank(a.getPassword())) {
            errors.rejectValue("password", "app.user.profile.password.empty");
        }
        if (a.getPassword() != null && !a.getPassword().equals(a.getPassword2())) {
            errors.rejectValue("password", "app.user.profile.password.mismatch");
        }
    }


    public void setNewUser(boolean newUser) {
        this.newUser = newUser;
    }

    public void setUserManager(UserManager userManager) {
        this.userManager = userManager;
    }
}
